package main

import (
	"context"
	"log"
	"time"

	pb "gitee.com/dingdang4D/go-basic-exercises/net/grpc/proto/calculator"

	"google.golang.org/grpc"
)

func main() {
	// 连接到 gRPC 服务器
	conn, err := grpc.Dial("localhost:50051", grpc.WithInsecure(), grpc.WithBlock())
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c := pb.NewCalculatorClient(conn)

	// 创建上下文
	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()

	// 调用 Add 方法
	r, err := c.Add(ctx, &pb.AddRequest{A: 3, B: 4})
	if err != nil {
		log.Fatalf("could not add: %v", err)
	}
	log.Printf("Add: %d", r.GetResult())
}
